﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace SmartBlast
{
    public partial class frmExpenditures : Form
    {
        private static string TABLE_NAME = "Expenditures";
        private static string[] HEADERS = new string[] { "Name", "Date", "Description", "Amount"};
        private static string[] FIELD_NAMES = new string[] { "Name", "Date", "Description", "Amount" };
        private string[] fields;
        private DataTable expendituresDataTable;
        private bool editable = false;
        private DataBase_BUS databaseBUS;
        private Main main;

        public frmExpenditures(Main main)
        {
            this.main = main;
            InitializeComponent();
            databaseBUS = DataBase_BUS.getInstance();
           
        }

        private void btn_search_Click(object sender, EventArgs e)
        {
            collectFields();
            expendituresDataTable = databaseBUS.selectRecrordFromTable(TABLE_NAME, FIELD_NAMES, fields);
            refresh();
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            if (!noFieldsAreBlank()) {
                MessageBox.Show("\nPlease input all fields", "Missing Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            else
            {
                collectFields();
                try
                {
                   databaseBUS.insertRecordIntoTable(TABLE_NAME, fields);
                    clearTextBoxes();
                    showAll();
                }
                catch
                {
                    gridExpenditures.Visible = false;
                    lbl_NoResults.Visible = true;
                    lbl_NoResults.Text = "There is an expenditure with the same name. \n Please select another";
                }

            }
        }

        private void btn_Clear_Click(object sender, EventArgs e)
        {
            clearTextBoxes();
        }

        private void btn_showAll_Click(object sender, EventArgs e)
        {
            showAll();
        }

        private void collectFields()
        {
            fields = new string[] { "'" + txtName.Text + "'",
                (dateExpDate.Checked==true)? "'"+dateExpDate.Value.ToString()+"'" : "",
                "'"+txtDescription.Text+"'", txtAmount.Text};
        }

        private Boolean noFieldsAreBlank()
        {
            return (txtAmount.Text != "" && txtName.Text != "" && txtDescription.Text != "" &&
                dateExpDate.Checked == true);
        }

        private void clearTextBoxes()
        {
            txtAmount.Text = txtName.Text = txtDescription.Text = "";
            dateExpDate.Checked = false;
        }

        private void showAll()
        {
            expendituresDataTable = databaseBUS.selectRecrordFromTable(TABLE_NAME, null, null);
            refresh();
        }


        private void refresh()
        {
            if (expendituresDataTable.Rows.Count == 0)
            {
                gridExpenditures.Visible = false;
                lbl_NoResults.Visible = true;
            }
            else
            {
                lbl_NoResults.Visible = false;
                gridExpenditures.Visible = true;
                gridExpenditures.DataSource = expendituresDataTable;
                gridExpenditures.Columns[3].ReadOnly = true; 
                for (int i = 3; i < gridExpenditures.ColumnCount; i++)
                    gridExpenditures.Columns[i].HeaderText = HEADERS[i - 3];
            }
        }

        private void gridExpenditures_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            string name = ((System.Windows.Forms.DataGridView)(sender)).CurrentRow.Cells[3].Value.ToString();
            int index = (((System.Windows.Forms.DataGridView)(sender)).CurrentCell).ColumnIndex;

            if (index == 0) //click on "edit"
            {
                ((System.Windows.Forms.DataGridView)(sender)).CurrentRow.DefaultCellStyle.ForeColor = Color.Red;
                editable = true;
            }

            else if (index == 1 && editable) //click on "save"
            {
                string date = ((System.Windows.Forms.DataGridView)(sender)).CurrentRow.Cells[4].Value.ToString();
                string description = ((System.Windows.Forms.DataGridView)(sender)).CurrentRow.Cells[5].Value.ToString();
                string amount = ((System.Windows.Forms.DataGridView)(sender)).CurrentRow.Cells[6].Value.ToString();



                fields = new string[] { "'" + name + "'", "'" + date + "'", "'" + description + "'", amount };

                databaseBUS.updateRecordInTable(TABLE_NAME, FIELD_NAMES, fields, FIELD_NAMES[0], fields[0]);

                MessageBox.Show("The expenditure was successfully updated");
                showAll();
                editable = false;
            }

            else if (index == 2) //click on "delete"
            {
                DialogResult dialogResult = MessageBox.Show("Are you sure you want to delete the expenditure?", "Delete", MessageBoxButtons.YesNo);
                if (dialogResult == DialogResult.Yes)
                {
                    databaseBUS.deleteRecordFromTable(TABLE_NAME, FIELD_NAMES[0], "'"+name+"'");
                    MessageBox.Show("The expenditure was successfully deleted");
                }
                               
                showAll();
            }
        }

        private void txtId_TextChanged(object sender, EventArgs e)
        {
            int id;
            bool success = int.TryParse(txtAmount.Text, out id);
            String prevText = txtAmount.Text.Substring(0, Math.Max(0, txtAmount.Text.Length - 1));
            if (!success)
            {
                txtAmount.Text = prevText;
            }
            else
            {
                txtAmount.Text = id.ToString();
            }
        }


        private void btn_exit_Click(object sender, EventArgs e)
        {
            
            this.Hide();
        }


        private void frmexpenditures_Load(object sender, EventArgs e)
        {
            dateExpDate.Checked = false;
        }

        private void frmExpenditures_Load_1(object sender, EventArgs e)
        {
            this.CenterToScreen();
        }


    }
}
